package com.qf.zyh.services.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.zyh.common.ResponseResult;
import com.qf.zyh.common.ResultType;
import com.qf.zyh.dao.AdminUserMapper;
import com.qf.zyh.dao.UserRoleMapper;
import com.qf.zyh.entity.AdminUser;
import com.qf.zyh.services.AdminUserServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminUserServicesImpl implements AdminUserServices {
    @Autowired
    private AdminUserMapper adminUserMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Override
    public AdminUser getUserByEmpwd(String email,String password) {
        AdminUser userByEm = adminUserMapper.getUserByEm(email);
        if (userByEm!=null&&userByEm.getPassword().equals(password)) {
            return userByEm;
        }
        return null;
    }
    @Override
    public AdminUser getUserByEm(String email) {
        AdminUser userByEm = adminUserMapper.getUserByEm(email);
        return userByEm;
    }

    @Override
    public ResultType table(Integer page, Integer limit, String realName, String email, Integer status) {
        PageHelper.startPage(page,limit);
        List<AdminUser> adminUsers=adminUserMapper.table(realName,email,status);
        PageInfo pageInfo = new PageInfo(adminUsers);
        return new ResultType(0,"",(int)pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public ResponseResult getassign(Integer userId, Integer[] roleIds) {
        userRoleMapper.deleteByUserId(userId);
        userRoleMapper.addUserRole(userId,roleIds);
        return ResponseResult.getSuccess();
    }

    @Override
    public List<Integer> getuserRole(Integer userId) {
        return userRoleMapper.getAll(userId);
    }

    @Override
    public ResponseResult addUser(AdminUser adminUser) {
        if (adminUser!=null) {
            int i = adminUserMapper.insertSelective(adminUser);
            if (i>0) {
                return ResponseResult.getSuccess();
            }
        }
        return ResponseResult.getFail("新增失败");
    }

    @Override
    public ResponseResult updateUser(AdminUser adminUser) {
        if (adminUser!=null) {
            int i = adminUserMapper.updateByPrimaryKeySelective(adminUser);
            if (i>0) {
                return ResponseResult.getSuccess();
            }
        }
        return ResponseResult.getFail("修改失败");
    }

    @Override
    public ResponseResult delUser(Integer[] ids) {
        int i = adminUserMapper.deleteById(ids);
        return ResponseResult.getSuccess();
    }
}
